﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace Quartz.WebManager.BuildExt
{
    /// <summary>
    /// Provides assertion methods for check files.
    /// </summary>
    internal static class FileAssert
    {
        /// <summary>
        /// Checks that two files have the exact same content.
        /// </summary>
        /// <param name="expectedPath">The path of the file containing the expected content.</param>
        /// <param name="actualPath">The path of the actual file.</param>
        public static void AreEqual(string expectedPath, string actualPath)
        {
            byte[] expectedBuffer = new byte[1024];
            byte[] actualBuffer = new byte[1024];
            long current = 0;
            using (Stream expected = new FileStream(expectedPath, FileMode.Open, FileAccess.Read))
            using (Stream actual = new FileStream(actualPath, FileMode.Open, FileAccess.Read))
            {
                int expectedLength = expected.Read(expectedBuffer, 0, expectedBuffer.Length);
                int actualLength = actual.Read(actualBuffer, 0, actualBuffer.Length);

                while (expectedLength > 0)
                {
                    // TODO: provides better messages
                    Assert.AreEqual(expectedLength, actualLength);
                    CollectionAssert.AreEqual(expectedBuffer, actualBuffer);

                    current += expectedLength;
                    expectedLength = expected.Read(expectedBuffer, 0, expectedBuffer.Length);
                    actualLength = actual.Read(actualBuffer, 0, actualBuffer.Length);
                }

                // TODO: provides better messages
                Assert.AreEqual(expectedLength, actualLength);
            }
        }
    }
}
